Visual Basic Examples

To use the SFPack Library in your Visual Basic program you must first add a reference to the library. Open the References Dialog and check Megota Software SFPack Library (1.0).

Packing a SF2 File

This code sample shows how you can pack a SF2 file to a SFPack File.

Private WithEvents e As Engine
Private sf As SF2File
Sub CompressFile (cSF2File As String, cSFPackFile As String, _
cText As String, cLicense As String, cPassword as String)
    Set e = new Engine
Set sf = e.OpenSF2 (cSF2File)
    ' Check if everything is OK
If sf Is Nothing Then
MsgBox e.ErrorStr
Exit Sub
End If


With sf
        ' Now set the properties for the SFPack File
.Text = cText
.License = cLicense
.Password = cPassword

' Compress to SFPack File
if Not .Pack (cSFPackFile) Then
MsgBox .ErrorStr
End If

End With

End Sub

 

Unpacking a SFPack File

This sample shows how to unpack a SFPack File

Private WithEvents e As Engine
Private sp As SFPackFile
Sub InflateFile (cSFPackFile As String, cSF2File As String)
    Set e = new Engine
Set sp = e.OpenSFPack (cSFPackFile)
    ' Check if everything is OK
If sp Is Nothing Then
MsgBox e.ErrorStr
Exit Sub
End If


With sp
        ' Display the Text (if any)
If .Text <> "" Then
MsgBox .Text
Endif

' Display the License (if any)
If .License <> "" Then
If MsgBox (.License, vbYesNo, "Do you agree?") = vbYes Then
.AgreeLicense
End If
End If
        If .IsEncrypted Then
.Password = InputLine ("Password", "File is encrypted!")
End If
        ' Inflate to SF2 File       
if Not .Inflate (cSF2File) Then
MsgBox .ErrorStr
End If

End With

End Sub

 

Using the Progress event

With the help of the progress event you can do background processing while the Inflate or Pack method is running. In this example it is assumed that a dialog box is displayed, which has a ProgressBar pb and a Button btCancel. Note also the declaration of e with the WithEvents keyword.

Private WithEvents e As Engine
Private sp As SFPackFile
Private bCancel As Boolean
Private Sub btCancel_Click()
    bCancel = TRUE
End Sub
Private Sub e_Progress (ByVal nPercent As Integer)
    ' Update the progress bar display
pb.Value = nPercent

' Cancel the operation if somebody changed bCancel
' sp.Error will return peCanceled after a sp.Cancel
if bCancel Then
sp.Cancel
End If

DoEvents

End Sub